*&---------------------------------------------------------------------*
*& Report Y_ALV_MODEL2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT y_alv_model2.

TABLES: ekko ,sscrfields .
DATA: BEGIN OF g_data,
        sel     TYPE char1,
        status  TYPE char4,
        message TYPE char100,
        ebeln   TYPE ebeln,
      END OF g_data.

DATA: gt_data LIKE TABLE OF g_data,
      gs_data LIKE LINE OF gt_data.

DATA: gv_ucomm TYPE   sy-ucomm.

" 选择屏幕
SELECTION-SCREEN BEGIN OF BLOCK blk01.

  SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

SELECTION-SCREEN END OF BLOCK blk01.


CLASS lcl_data DEFINITION .

  PUBLIC SECTION .

    METHODS: constructor,
      get_data .

  PRIVATE SECTION .

ENDCLASS .

CLASS lcl_alv DEFINITION .

  PUBLIC SECTION .
    DATA:
      lt_fieldcat TYPE lvc_t_fcat,
      ls_fieldcat TYPE lvc_s_fcat,
      ls_layout_s TYPE lvc_s_layo,
      lt_exclude  TYPE ui_functions.

    METHODS:
      handle_toolbar
        FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING e_object e_interactive,
      handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING e_ucomm,
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING er_data_changed ,
      handle_double_click
        FOR EVENT double_click OF cl_gui_alv_grid
        IMPORTING e_row e_column es_row_no ,
      register_event ,
      field_catalog ,
      display ,
      exclude_fun ,
      select_all_items,
      not_select_all_items ,
      add_field IMPORTING pv_field TYPE char30
                          pv_text  TYPE char30
                          pv_len   TYPE i
                          pv_check TYPE char1
                          pv_edit  TYPE char1
                .
  PRIVATE SECTION.
    DATA: mo_grid TYPE REF TO cl_gui_alv_grid..

ENDCLASS .

INITIALIZATION.


START-OF-SELECTION.

  DATA(lo_obj) = NEW lcl_data( ) .
  DATA(lo_alv) = NEW lcl_alv( ) .

  lo_obj->get_data( ) .
  lo_alv->display( ) .


CLASS lcl_data IMPLEMENTATION.

  METHOD constructor.
  ENDMETHOD .

  METHOD get_data.

    SELECT *
      INTO CORRESPONDING FIELDS OF TABLE gt_data
       FROM ekko
      WHERE ebeln IN s_ebeln .

  ENDMETHOD.

ENDCLASS .


CLASS lcl_alv IMPLEMENTATION.

  METHOD add_field.

    CLEAR ls_fieldcat.
    ls_fieldcat-fieldname    = pv_field.
    ls_fieldcat-scrtext_l    = pv_text.
    ls_fieldcat-outputlen    = pv_len.
    ls_fieldcat-checkbox    = pv_check.
    ls_fieldcat-edit        = pv_edit.

    APPEND ls_fieldcat TO lt_fieldcat.

  ENDMETHOD .

  METHOD field_catalog.

    CLEAR: ls_layout_s.
    ls_layout_s-sel_mode = 'A'.
    ls_layout_s-cwidth_opt = 'X'.

    CLEAR: lt_fieldcat .

    add_field( pv_field = 'SEL' pv_text = '复选框' pv_len = '1' pv_check = 'X' pv_edit = 'X' ).
    add_field( pv_field = 'STATUS' pv_text = '状态' pv_len = '4'  pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'MESSAGE' pv_text = '消息' pv_len = '30'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'EBELN' pv_text = '采购订单' pv_len = '20'   pv_check = '' pv_edit = ''  ).

  ENDMETHOD.

  METHOD exclude_fun.

    CLEAR: lt_exclude .

    DATA: lv_exclude TYPE ui_func .
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_print.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_info.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
    APPEND lv_exclude TO lt_exclude.

  ENDMETHOD.

  METHOD display .

    field_catalog( ) .
    exclude_fun( ) .

    IF mo_grid IS INITIAL.

      " create object mo_grid
      register_event( ) .

      mo_grid->set_table_for_first_display(
         EXPORTING   i_save               = 'A'
                    is_layout            = ls_layout_s
                    it_toolbar_excluding = lt_exclude
               CHANGING
                    it_outtab            = gt_data
                    it_fieldcatalog      = lt_fieldcat ) .

      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.

    ELSE.

      mo_grid->refresh_table_display( ) .

    ENDIF.

    CALL SCREEN '0900'.

  ENDMETHOD .


  METHOD handle_toolbar.

    DATA: ls_toolbar TYPE stb_button .

    CLEAR: ls_toolbar.
    MOVE   'ALL'     TO   ls_toolbar-function.
    MOVE   '全选'   TO   ls_toolbar-text.
    MOVE    '@4B@'   TO   ls_toolbar-icon .
    APPEND ls_toolbar TO e_object->mt_toolbar .

    CLEAR: ls_toolbar.
    MOVE   'SAL'     TO   ls_toolbar-function.
    MOVE   '全不选'   TO   ls_toolbar-text.
    MOVE    '@4D@'   TO   ls_toolbar-icon .
    APPEND ls_toolbar TO e_object->mt_toolbar .


    CLEAR: ls_toolbar.
    MOVE   'EXECUTE'     TO   ls_toolbar-function.
    MOVE   '执行'   TO   ls_toolbar-text.
    MOVE    '@4A@'   TO   ls_toolbar-icon .
    APPEND ls_toolbar TO e_object->mt_toolbar .


  ENDMETHOD.                    "handle_onf4

  METHOD handle_user_command.

    CASE e_ucomm.
      WHEN 'EXECUTE'.

      WHEN 'ALL'.
        select_all_items( ) .
      WHEN 'SAL'.
        not_select_all_items( ) .
      WHEN OTHERS.
    ENDCASE.

    CALL METHOD mo_grid->refresh_table_display
      EXPORTING
        i_soft_refresh = 'X'
      EXCEPTIONS
        finished       = 1
        OTHERS         = 2.

  ENDMETHOD.

  METHOD handle_double_click.
  ENDMETHOD.

  METHOD handle_data_changed .
  ENDMETHOD .


  METHOD register_event .

    IF mo_grid IS INITIAL .

      DATA(lo_container) = NEW cl_gui_custom_container( container_name = 'CONTAINER' ) .
      mo_grid = NEW cl_gui_alv_grid( i_parent = lo_container ) .

    ENDIF.

    SET HANDLER me->handle_toolbar FOR mo_grid .
    SET HANDLER me->handle_user_command FOR mo_grid .
    SET HANDLER me->handle_double_click FOR mo_grid.
    SET HANDLER me->handle_data_changed FOR mo_grid .

    CALL METHOD mo_grid->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_modified
      EXCEPTIONS
        error      = 1
        OTHERS     = 2.

  ENDMETHOD .


  METHOD select_all_items .
    LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
      <fs_data>-sel = 'X' .
    ENDLOOP.
  ENDMETHOD .
  METHOD  not_select_all_items .
    LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
      <fs_data>-sel = '' .
    ENDLOOP.
  ENDMETHOD .

ENDCLASS .



MODULE status_0900 OUTPUT.

  SET PF-STATUS 'STA_0900'.
  SET TITLEBAR 'TITLE_0900'.

ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0900  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0900 INPUT.

  CASE gv_ucomm.
    WHEN 'BACK' OR 'EXIT'.
      LEAVE TO SCREEN 0.
    WHEN 'CANCEL'.
      LEAVE PROGRAM.
    WHEN OTHERS.
  ENDCASE.

ENDMODULE.